﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class PublisherManager
    {
        public PublisherInfo GetPublisherById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryPublishers = from d in db.Publishers
                                   where d.Status.Equals(1)
                                   where d.Id.Equals(id)
                                      select new PublisherInfo { Id = d.Id, Name = d.Name, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate };

                return queryPublishers.First();
            }

        }


        public IList<PublisherInfo> ListPublisher()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryPublishers = from d in db.Publishers
                                   select new PublisherInfo { Id = d.Id, Name = d.Name, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate };

                return queryPublishers.ToList();
            }
        }


        public void CreatePublisher(Publisher Publishers)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Publishers.InsertOnSubmit(Publishers);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Publishers"></param>
        public void UpdatePublisher(Publisher Publishers)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Publishers = db.Publishers.Single(d => d.Id.Equals(Publishers.Id));

                _Publishers.Name = Publishers.Name;
                _Publishers.Description = Publishers.Description;
                _Publishers.CreatedBy = Publishers.CreatedBy;
                _Publishers.CreatedDate = Publishers.CreatedDate;
                _Publishers.ModifiedBy = Publishers.ModifiedBy;
                _Publishers.ModifiedDate = Publishers.ModifiedDate;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeletePublisher(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Publishers = db.Publishers.Single(d => d.Id.Equals(id));

                db.Publishers.DeleteOnSubmit(_Publishers);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameExisted(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Publishers
                            where d.Name.Equals(name)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(string name, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Publishers
                            where d.Name.Equals(name)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }


    }
}
